package com.yang.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontProvider;
import com.itextpdf.text.html.simpleparser.StyleSheet;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

@SuppressWarnings("deprecation")
public class GeneratePDF {

	protected StyleSheet  style;
	protected HashMap<String,Object> providers = new HashMap<String, Object>();
	
	public void setStyle(StyleSheet style) {
		this.style = style;
	}
	
	final class Pro implements FontProvider{

		public boolean isRegistered(String fontname) {
			return false;
		}

		public Font getFont(String fontname, String encoding, boolean embedded,
				float size, int style, BaseColor color) {
			BaseFont bf = null;
			try {
				bf = BaseFont.createFont("C:/Windows/Fonts/simfang.ttf",
						BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
			} catch (DocumentException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return new Font(bf,size,style,color);
		}
		
	}
	public void setProviders(HashMap<String, Object> providers) {
		this.providers = providers;
	}

	public  void generatePDF(OutputStream os) throws DocumentException, IOException, TemplateException{
		
		Configuration cfg = new Configuration();
		cfg.setDirectoryForTemplateLoading(new File("D://project//Tes/WebRoot/template"));
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		cfg.setDefaultEncoding("UTF-8");
		
		Template template=cfg.getTemplate("index.html");
		String filePath="d:/tmp/a.html";
		
		Writer out = new BufferedWriter(new FileWriter(filePath));
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("name", "tom");
		template.process(map, out);
		out.flush();
		out.close();
		Document document = new Document();
		// step 2
		PdfWriter writer=PdfWriter.getInstance(document, os);
		// step 3
		document.open();
		XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(filePath),null,new Pro());
		// step 5
		document.close();
		/*read.close();*/
		File file=new File(filePath);
		if(file.isFile()){
			file.delete();
		}
	}
	public static void main(String[] args) throws DocumentException,
			IOException, TemplateException {
		new GeneratePDF().generatePDF(new FileOutputStream("d:/a.pdf"));
		//generateFile();
	}

	public StyleSheet getStyle() {
		return style;
	}

	public HashMap<String, Object> getProviders() {
		return providers;
	}
}
